/**
 * 开发人员：yhb
 * 时间：2011-7-28 8:51:32
 */
package com.towker.core.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * 模型工具类。
 * @author yanghb
 */
public class ModelUtil
{
	/**
	 * 将对象的字符串形式显示出来："{id:'1',name:'aaa'...}"。
	 * @param obj
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static String toString(Object obj)
	{
		Class c = obj.getClass();
		Field[] fs = c.getDeclaredFields();
		List list = new ArrayList();
		for (Field f : fs)
		{
			f.setAccessible(true);
			try
			{
				Object tmp = f.get(obj);
				String str = tmp.toString();
				list.add(f.getName() + ":'" + str + "'");
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}
		return list.toString().replace("[", "{").replace("]", "}");
	}

	@SuppressWarnings("unchecked")
	public static <T> T clone(T t, Boolean... isFilter)
	{
		Class c = t.getClass();
		T copy = null;
		try
		{
			copy = (T) c.newInstance();
			Class copyC = copy.getClass();
			Method[] copyMs = copyC.getDeclaredMethods();
			for (Method m : copyMs)
			{
				String mName = m.getName();
				Method[] cMs = copyC.getDeclaredMethods();
				for (Method cM : cMs)
				{
					String cmName = cM.getName();
					if (mName.replace("set", "").equals(cmName.replace("get", "")))
					{
						int status = 0;
						while (true)
						{
							if(6<=status){
								break;
							}
							try
							{
								Object obj=cM.invoke(t, new Object[]{});
								if(0<isFilter.length&&isFilter[0])
								{
									obj =EscapeUtil.escapeXml(obj);
								}
								switch (status)
								{
									case 0:
										m.invoke(copy, (String) (obj));
										break;
									case 1:
										m.invoke(copy, (Integer) (obj));
										break;
									case 2:
										m.invoke(copy, (Long)(obj));
										break;
									case 3:
										m.invoke(copy, (Float)(obj));
										break;
									case 4:
										m.invoke(copy, (Double)(obj));
										break;
									case 5:
										m.invoke(copy, (Boolean) (obj));
										break;
								}
								break;
							}
							catch (Exception e)
							{
								status++;
							}
						}
					}
				}
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return copy;
	}
	
	@SuppressWarnings("unchecked")
	public static <T> T clone(Object src,Class<T> clz, Boolean... isFilter)
	{
		Class cSrc = src.getClass();
		T copy = null;
		try
		{
			copy = (T) clz.newInstance();
			Class cCopy = copy.getClass();
			Method[] msSrc = cSrc.getDeclaredMethods();
			for (Method  mSrc: msSrc)
			{
				String nameSrc = mSrc.getName();
				Method[] msCopy = cCopy.getMethods();
				for (Method mCopy : msCopy)
				{
					String nameCopy = mCopy.getName();
					if (nameSrc.startsWith("get")&&nameCopy.startsWith("set")&&nameSrc.replace("get", "").equals(nameCopy.replace("set", "")))
					{
						int status = 0;
						while (true)
						{
							try
							{
								Object obj=mSrc.invoke(src, new Object[]{});
								if(null!=isFilter[0]&&isFilter[0])
								{
									obj =EscapeUtil.escapeXml(obj);
								}
								switch (status)
								{
									case 0:
										mCopy.invoke(copy, (String) (obj));
										break;
									case 1:
										mCopy.invoke(copy, (Integer) (obj));
										break;
									case 2:
										mCopy.invoke(copy, (Long)(obj));
										break;
									case 3:
										mCopy.invoke(copy, (Float)(obj));
										break;
									case 4:
										mCopy.invoke(copy, (Double)(obj));
										break;
									case 5:
										mCopy.invoke(copy, (Boolean) (obj));
										break;
								}
								break;
							}
							catch (Exception e)
							{
								status++;
							}
						}
					}
				}
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return copy;
	}
}
